package com.sci99.ex.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;

import com.sci99.ex.model.Category;
import com.sci99.ex.util.MyBatisUtil;

public class CategoryService {
	static Logger log = Logger.getLogger(CategoryService.class);

	public static List<Category> getCategoryList() throws Exception {
		SqlSession s = MyBatisUtil.getSession();
		List<Category> rs = s.selectList("getCategory");
		s.close();
		return rs;
	}

	// getCategoryActive
	public static List<Category> getCategoryActiveList() throws Exception {
		SqlSession s = MyBatisUtil.getSession();
		List<Category> rs = s.selectList("getCategoryActive");
		s.close();
		return rs;
	}

	public static List<Category> getCategoryList(String name) throws Exception {
		Map<String, String> arg = new HashMap<>(2, 1);
		arg.put("name", name);
		SqlSession s = MyBatisUtil.getSession();
		List<Category> rs = s.selectList("getCategory", arg);
		s.close();
		return rs;
	}

	public static void saveCategory(Category cate) throws Exception {
		SqlSession s = MyBatisUtil.getSession();
		if (cate.getId() == null) {
			s.insert("addCategory", cate);
		} else {
			s.update("uptCategory", cate);
		}
		s.commit();
		s.close();
	}

	public static void delCategory(Category cate) throws Exception {
		SqlSession s = MyBatisUtil.getSession();
		s.delete("delCategory", cate);
		s.commit();
		s.close();
	}
}
